﻿using Chromely.Core.Logging;
using Chromely.Core.Network;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Reader.Core
{
    public static class Utility
    {
        public static int GetNotUsedPort()
        {
            var ipProperties = IPGlobalProperties.GetIPGlobalProperties();
            var ipEndPoints = ipProperties.GetActiveTcpListeners();
            var port = new Random().Next(10000, 65535);
            while (ipEndPoints.Any(iep => iep.Port == port))
            {
                port = new Random().Next(10000, 65535);
            }
            return port;
        }
        public static async Task<string> GetEmbedStringSourceAsync(string path)
        {
            Assembly assembly = Assembly.GetEntryAssembly();
            using var stream = assembly.GetManifestResourceStream($"{assembly.EntryPoint.DeclaringType?.Namespace}.Embed.{path}");
            if (stream != null)
            {
                using var reader = new StreamReader(stream);
                return await reader.ReadToEndAsync();
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
